Zepto(function($) {
  window.onload=function () {
    init();
    /**
     * 获取token  存放在本地 localStorage
     *
     */
    // saveToken();
  }
  /*
  * 品牌馆
  * */
  function init() {
    // 获取类目列表
    //categary();
    //获取品牌列表
    //Getbrandlist();
    //获取大家都在买列表
    weShopping();
    bgupInitData();
  }
  /**
   * 冰果优品商品列表
   */
  function bgupGetData() {
    // $.ajax({
    //   type: 'GET',
    //   url: getUrl + '/wxmall/brand/hotlist',
    //   data: {},
    //   dataType: 'json',
    //   timeout: 15000,
    //   context: $('body'),
    //   success: function(data){
    //     console.log(data);
    //   },
    //   error: function(xhr, type){
    //   }
    // });
    bgupInitData();
  }
  function bgupInitData() {
    var table = document.body.querySelector('.mui-table-view1');
    var cells = document.body.querySelectorAll('.mui-table-view-cell1');
    for (var i = cells.length, len = i + 6; i < len; i++) {
      var li = document.createElement('li');
      li.className = 'mui-table-view-cell1';
      li.innerHTML='<li>'+
          '<div class="play-more"></div>'+
          '<div class="e-video">'+
          '<img src="http://dev-bucket.bingstar.com.cn/uploadImage/2017/4/24/1492998799456.png">'+
          '</div>'+
          '<div class="specifica-goods">'+
          '<div class="goods-title">飞利浦（PHILIPS）电动剃须刀'+ i +'</div>'+
          '<div class="goods-detail">'+
          '<div class="goods-price">'+
          '<span class="reality-goods-price">￥100</span>'+
          '<span class="original-goods-price">￥200</span>'+
          '</div>'+
          '<div class="add-icon">'+
          '<div class="add-cart"></div>'+
          '<div class="selec-details"></div>'+
          '</div>'+
          '</div>'+
          '</div>'+
          '</li>';
      table.appendChild(li);
    }
  }

  function categary() {
    // $.ajax({
    //   type: "GET",
    //   url: getUrl + '/wxmall/categorygoods/list',
    //   data: {},
    //   headers: headers("categorygoods.list"),
    //   dataType: 'json',
    //   timeout: 15000,
    //   context: $('body'),
    //   success: function(data){
    //     categaryInit(data);
    //   },
    //   error: function(xhr, type){
    //   }
    // })
  }
  /**
   * 初始化类目列表
   * @param data
   */
  function categaryInit(data) {
    $(".categary").html("");
    if(!data.success){
      return
    }
    var data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < data1.length; i++){
        var cateName = "";
        if(data1[i].cateName != null){
          cateName = data1[i].cateName;
        }
        html += '<div data-cata="'+data1[i].cateId+'" class="swiper-slide">'+cateName+'</div>';
      }
      $("#categary").append(html);
    }
  }

  /**
   * 获取大家都在买列表
   *
   */
  function weShopping() {
    // $.ajax({
    //   type: 'GET',
    //   url: getUrl + '/wxmall/goods/selectEveryBodyBuyList',
    //   data: {limit:100},
    //   dataType: 'json',
    //   timeout: 15000,
    //   headers: headers("goods.selectEveryBodyBuyList"),
    //   context: $('body'),
    //   success: function(data){
    //     weShoppingInit(data);
    //   },
    //   error: function(xhr, type){
    //
    //   }
    // })
    var html="";
    for(var i = 0; i < 10; i++){

      html += '<li>'+
          '<div data-goodId="goodsId" class="e-video">'+
          '<img src="http://dev-bucket.bingstar.com.cn/uploadImage/2017/4/27/1493254571322.png">'+
          '</div>'+
          '<div class="specifica-goods">'+
          '<div class="goods-title">链鱼</div>'+
          '<div class="goods-detail">'+
          '<div class="goods-price">'+
          '<span class="reality-goods-price">￥18.00</span>'+
          '<span class="original-goods-price">￥20.00</span>'+
          '<span class="original-goods-price1"></span>'+
          '</div>'+
          '<div class="add-icon">'+
          '<div class="add-cart"></div>'+
          '<div class="selec-details"></div>'+
          '</div>'+
          '</div>'+
          '</div>'+
          '</li>';
    }
    $("#weshoppind").append(html);
  }

  /**
   * 我们都在买列表
   *
   * @param data
   */
  function weShoppingInit(data) {
    console.log(data);
    $("#weshoppind").html("");
    if(!data.success){
      return
    }
    var data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < data1.length; i++){
        var limitPrice = "";
        if(data1[i].limitPrice != null){
          limitPrice = "￥" + data1[i].limitPrice;
        }
        var salePrice = ""
        if(data1[i].salePrice != null){
          salePrice = data1[i].salePrice;
        }
        var goodsName = ""
        if(data1[i].goodsName != null){
          goodsName = data1[i].goodsName;
        }
        //
        var goodsId = ""
        if(data1[i].goodsId != null){
          goodsId = data1[i].goodsId;
        }
        var goodsImage = ""
        if(data1[i].goodsImage != null && data1[i].goodsImage.length != 0){
          if(data1[i].goodsImage[0].picUrl != null){
            goodsImage = imgUrl +  data1[i].goodsImage[0].picUrl;
          }
        }
        html += '<li>'+
            '<div data-goodId="'+goodsId+'" class="e-video">'+
            '<img src="'+ goodsImage +'">'+
            '</div>'+
            '<div class="specifica-goods">'+
            '<div class="goods-title">'+ goodsName+'</div>'+
            '<div class="goods-detail">'+
            '<div class="goods-price">'+
            '<span class="reality-goods-price">'+ salePrice+'</span>'+
            '<span class="original-goods-price">'+ limitPrice +'</span>'+
            '<span class="original-goods-price1"></span>'+
            '</div>'+
            '<div class="add-icon">'+
            '<div class="add-cart"></div>'+
            '<div class="selec-details"></div>'+
            '</div>'+
            '</div>'+
            '</div>'+
            '</li>';
      }
      $("#weshoppind").append(html);
    }
  }
  function Getbrandlist() {
    // $.ajax({
    //   type: 'GET',
    //   url: getUrl + '/wxmall/brand/hotList',
    //   data: {},
    //   dataType: 'json',
    //   timeout: 15000,
    //   headers: headers("brand.hotList"),
    //   context: $('body'),
    //   success: function(data){
    //     barndListInit(data);
    //   },
    //   error: function(xhr, type){
    //     // alert('Ajax error!')
    //   }
    // });
  }
  var data1 = [];
  function barndListInitSq() {
    $("#brandList").html("");
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      var length = "";
      if(data1.length >=8){
        length = 8
      }else{
        length = data1.length;
      }
      for(var i = 0; i < length; i++){
        var brandUrl = "";
        if(data1[i].brandUrl != null){
          brandUrl = imgUrl + data1[i].brandUrl;
        }
        var brandId = "";
        if(data1[i].brandId != null){
          brandId = imgUrl + data1[i].brandId;
        }
        var brandName = "";
        if(data1[i].brandName != null){
          brandName = data1[i].brandName;
        }
        html += '<img data-brandName="'+brandName+'" data-brandId="'+data1[i].brandId+'" data-brandUrl="'+ brandUrl +'" src="'+brandUrl+'">';
      }
      $("#brandList").append(html);
    }
  }
  function barndListInit(data) {
    $("#brandList").html("");
    if(!data.success){
      return
    }
    data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      var length = "";
      if(data1.length >=8){
        length = 8;
      }else{
        length = data1.length;
      }
      for(var i = 0; i < length; i++){
        var brandUrl = "";
        if(data1[i].brandUrl != null){
          brandUrl = imgUrl + data1[i].brandUrl;
        }
        var brandId = "";
        if(data1[i].brandId != null){
          brandId = imgUrl + data1[i].brandId;
        }
        var brandName = "";
        if(data1[i].brandName != null){
          brandName = data1[i].brandName;
        }
        html += '<img data-brandName="'+brandName+'" data-brandId="'+data1[i].brandId+'" data-brandUrl="'+ brandUrl +'" src="'+brandUrl+'">';
      }
      $("#brandList").append(html);
    }
  }
  function barndListInitMore() {
    $("#brandList").html("");
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < length; i++){
        var brandUrl = "";
        if(data1[i].brandUrl != null){
          brandUrl = imgUrl + data1[i].brandUrl;
        }
        var brandId = "";
        if(data1[i].brandId != null){
          brandId = imgUrl + data1[i].brandId;
        }
        var brandName = "";
        if(data1[i].brandName != null){
          brandName = data1[i].brandName;
        }
        html += '<img data-brandName="'+brandName+'" data-brandId="'+data1[i].brandId+'" data-brandUrl="'+ brandUrl +'" src="'+brandUrl+'">';
      }
      $("#brandList").append(html);
    }
  }

  /**
   * 初始化底部导航栏选中
   *
   */
  $(".tab1").parent().addClass("active");
  /**
   * 初始化页面定位在顶部
   *
   * @type {number}
   */
  $("body").screenTop = 0;
  // 点击三个tab 页
  $(".tab").on("click","div",function () {
    $(".tab>div").removeClass("active");
    $(this).addClass('active');
  })

  $(".tab").on("click",".tab1",function () {
    $(".tab1-cont").css("display","block");
    $(".tab2-cont").css("display","none");
    $(".tab3-cont").css("display","none");
    $("title").html("冰果优品");
  })
  /**
   * 切换品牌馆
   */
  $(".tab").on("click",".tab2",function () {
    $("title").html("品牌馆");
    $(".tab1-cont").css("display","none");
    $(".tab2-cont").css("display","block");
    $(".tab3-cont").css("display","none");
    init();
    // 品牌馆 banner 图
    var mySwiper2 = new Swiper('.swiper-container2', {
      loop: true,
      autoplay: 3000,
      pagination: '.swiper-pagination2'
    });
    // 分类 banner 初始化
    var swiper = new Swiper('.swiper-container3', {
      slidesPerView: 5,
      paginationClickable: true,
      spaceBetween: 5
    });
  })
  $(".tab").on("click",".tab3",function () {
    $(".tab1-cont").css("display","none");
    $(".tab2-cont").css("display","none");
    $(".tab3-cont").css("display","block");
    $("title").html("每日特惠");
  })
  /**
   * 品牌馆跳转详情
   *
   */
  $(".tab2-cont").on("click",".e-video",function () {
    sessionStorage.setItem("brandGoodId", $(this)[0].getAttribute("data-goodId"));
    window.location.href = "/index/bgupGoodsDetails.html"
  })
  /**
   * 冰果优品 商品详情
   *
   */
  $(".tab1-cont").on("click",".e-video",function () {
    window.location.href = "/index/videogoodsdetails.html"
  })
  $(".tab1-cont").on("click",".play-more",function () {
    window.location.href = "/index/videogoodsdetails.html"
  })
  /**
   * 初始化banner 图
   *
   * @type {Window.Swiper|*}
   */
  var mySwiper = new Swiper('.swiper-container1', {
    loop: true,
    autoplay: 3000,
    pagination: '.swiper-pagination1'
  });
  /**
   * 查看跟多品牌
   */
  $(".title").on("click",".more-brand",function () {
    $(".more-brand").css("display","none");
    $("#packup").css("display","block");
    barndListInitMore();
  });
  /**
   * 收起更多品牌
   */
  $(".title").on("click","#packup",function () {
    $(".more-brand").css("display","block");
    $("#packup").css("display","none");
    barndListInitSq();
  });
  /**
   * 跳转热门品牌
   *
   */
  $(".brand-list").on("click","img",function () {
    sessionStorage.setItem("brandName",$(this)[0].getAttribute("data-brandName"));
    sessionStorage.setItem("brandId",$(this)[0].getAttribute("data-brandId"));
    sessionStorage.setItem("brandUrl",$(this)[0].getAttribute("data-brandUrl"));
    window.location.href = "/index/barndgoods.html"
  })
  $(".goods-model").on("click",".swiper-slide",function () {
    sessionStorage.setItem("cateName",$(this).html());
    sessionStorage.setItem("cateId",$(this)[0].getAttribute("data-cata"));
    window.location.href = "/index/modelgoods.html";
  })
});